legacy.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Legacy installation process, i.e. `setup.py install`.
  2. """
  3. import logging
  4. import os
  5. import sys
  6. from distutils.util import change_root
  7. from pip._internal.exceptions import InstallationError
  8. from pip._internal.utils.logging import indent_log
  9. from pip._internal.utils.misc import ensure_dir
  10. from pip._internal.utils.setuptools_build import make_setuptools_install_args
  11. from pip._internal.utils.subprocess import runner_with_spinner_message
  12. from pip._internal.utils.temp_dir import TempDirectory
  13. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  14. if MYPY_CHECK_RUNNING:
  15. from typing import List, Optional, Sequence
  16. from pip._internal.build_env import BuildEnvironment
  17. from pip._internal.models.scheme import Scheme
  18. logger = logging.getLogger(__name__)
  19. class LegacyInstallFailure(Exception):
  20. def __init__(self):
  21. # type: () -> None
  22. self.parent = sys.exc_info()
  23. def install(
  24. install_options, # type: List[str]
  25. global_options, # type: Sequence[str]
  26. root, # type: Optional[str]
  27. home, # type: Optional[str]
  28. prefix, # type: Optional[str]
  29. use_user_site, # type: bool
  30. pycompile, # type: bool
  31. scheme, # type: Scheme
  32. setup_py_path, # type: str
  33. isolated, # type: bool
  34. req_name, # type: str
  35. build_env, # type: BuildEnvironment
  36. unpacked_source_directory, # type: str
  37. req_description, # type: str
  38. ):
  39. # type: (...) -> bool
  40. header_dir = scheme.headers
  41. with TempDirectory(kind="record") as temp_dir:
  42. try:
  43. record_filename = os.path.join(temp_dir.path, 'install-record.txt')
  44. install_args = make_setuptools_install_args(
  45. setup_py_path,
  46. global_options=global_options,
  47. install_options=install_options,
  48. record_filename=record_filename,
  49. root=root,
  50. prefix=prefix,
  51. header_dir=header_dir,
  52. home=home,
  53. use_user_site=use_user_site,
  54. no_user_config=isolated,
  55. pycompile=pycompile,
  56. )
  57. runner = runner_with_spinner_message(
  58. "Running setup.py install for {}".format(req_name)
  59. )
  60. with indent_log(), build_env:
  61. runner(
  62. cmd=install_args,
  63. cwd=unpacked_source_directory,
  64. )
  65. if not os.path.exists(record_filename):
  66. logger.debug('Record file %s not found', record_filename)
  67. # Signal to the caller that we didn't install the new package
  68. return False
  69. except Exception:
  70. # Signal to the caller that we didn't install the new package
  71. raise LegacyInstallFailure
  72. # At this point, we have successfully installed the requirement.
  73. # We intentionally do not use any encoding to read the file because
  74. # setuptools writes the file using distutils.file_util.write_file,
  75. # which does not specify an encoding.
  76. with open(record_filename) as f:
  77. record_lines = f.read().splitlines()
  78. def prepend_root(path):
  79. # type: (str) -> str
  80. if root is None or not os.path.isabs(path):
  81. return path
  82. else:
  83. return change_root(root, path)
  84. for line in record_lines:
  85. directory = os.path.dirname(line)
  86. if directory.endswith('.egg-info'):
  87. egg_info_dir = prepend_root(directory)
  88. break
  89. else:
  90. message = (
  91. "{} did not indicate that it installed an "
  92. ".egg-info directory. Only setup.py projects "
  93. "generating .egg-info directories are supported."
  94. ).format(req_description)
  95. raise InstallationError(message)
  96. new_lines = []
  97. for line in record_lines:
  98. filename = line.strip()
  99. if os.path.isdir(filename):
  100. filename += os.path.sep
  101. new_lines.append(
  102. os.path.relpath(prepend_root(filename), egg_info_dir)
  103. )
  104. new_lines.sort()
  105. ensure_dir(egg_info_dir)
  106. inst_files_path = os.path.join(egg_info_dir, 'installed-files.txt')
  107. with open(inst_files_path, 'w') as f:
  108. f.write('\n'.join(new_lines) + '\n')
  109. return True